package org.springcloud.wisdom.spring_01_canteen.dishes.fordishes;

import org.apache.ibatis.session.SqlSession;
import org.springcloud.wisdom.spring_01_canteen.common.bean.ResponseData;
import org.springcloud.wisdom.spring_01_canteen.common.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/dishes")
public class DishesController extends BaseController {
    @Autowired
    private DishesService dishesService;
    //查看所有菜品
    @GetMapping("/foods")
    public ResponseData<Object> getDish(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "dname",required = false) String dname,
            @RequestParam(value = "mallid",required = false) Integer mallid){
        return returnObject(dishesService.getOnrePage(pageNum,pageSize,dname,mallid));
    }

    //查看所有菜品
    @GetMapping("/foods1")
    public ResponseData<Object> getDish1(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "dname",required = false) String dname){
        return returnObject(dishesService.getOnrePage1(pageNum,pageSize,dname));
    }
    @GetMapping("/all")
    public ResponseData<Object> cdishes(
            @RequestParam(value = "dname",required = false) String dname)
    {
        return returnObject(dishesService.dish(dname));
    }
    //根据mallid查菜品
    @GetMapping("/getall")
    public ResponseEntity<List<Dishes>> getDishesByMallId(
            @RequestParam("mallid") Integer mallid) {
        List<Dishes> dishes = dishesService.getDishesByMallId(mallid);
        if (dishes != null && !dishes.isEmpty()) {
            return ResponseEntity.ok(dishes);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    //添加所有菜品
    @PostMapping("/add")
    public ResponseData<Boolean> add(Dishes dishes){
        return returnBoolean(dishesService.adddish(dishes));
    }
    //删除菜品
    @DeleteMapping("/del/{did}")
    public ResponseData<Boolean> deldish(@PathVariable("did") Integer did){
        return returnBoolean(dishesService.deldish(did));
    }
    //修改所有菜品价格
    @PutMapping("/update")
    public ResponseData<Boolean> updatedish(@RequestBody Dishes dishes){
        return returnBoolean(dishesService.updatedish(dishes));
    }
    //修改商品
    @PutMapping("/updateAll")
    public ResponseData<Boolean> update(@RequestBody Dishes dishes){
        return returnBoolean(dishesService.update(dishes));
    }
    //修改状态
    @PutMapping("/status")
    public ResponseData<Boolean> updateDishStatus(@RequestBody Map<String, Object> data) {
        if(data == null || data.get("did") == null || data.get("status") == null) {
            return new ResponseData<>(0, "Invalid request data", null);
        }
        Integer did = (Integer) data.get("did");
        Integer status = (Integer) data.get("status");
        // 根据传入的status值，调用approve或reject方法
        boolean result;
        if(status == 1) {
            result = dishesService.approve(did);
        } else if(status == 0) {
            result = dishesService.reject(did);
        } else {
            return new ResponseData<>(0, "Invalid status value", null);
        }
        return new ResponseData<>(1, "Status updated successfully", result);
    }
    //同意
    @GetMapping("/approve")
    public ResponseData<Object> updateNotice(@RequestParam("did")Integer did){
        Dishes dishes=new Dishes();
        dishes.setDid(did);
        return  returnObject(dishesService.updateIsdel(dishes));
    }
    //拒绝
    @GetMapping("/reject")
    public ResponseData<Object> updateNotice2(@RequestParam("did")Integer did){
        Dishes dishes=new Dishes();
        dishes.setDid(did);
        return  returnObject(dishesService.updateIsdel1(dishes));
    }
    //查看商品数量
    @GetMapping("/count/dishes")
    public int countDishesByMall(@RequestParam Integer mallid) {
        return dishesService.countDishesByMallId(mallid);
    }
}
